package com.hhh.springai_test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.model.dto.communityTag.AddCommunityTagDTO;
import com.hhh.springai_test.model.dto.communityTag.UpdateCommunityTagDTO;
import com.hhh.springai_test.model.po.CommunityTag;
import com.hhh.springai_test.service.CommunityTagService;
import com.hhh.springai_test.mapper.CommunityTagMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 29915
* @description 针对表【community_tag(社区tag表)】的数据库操作Service实现
* @createDate 2025-02-02 11:24:02
*/
@Service
public class CommunityTagServiceImpl extends ServiceImpl<CommunityTagMapper, CommunityTag>
    implements CommunityTagService{

    @Override
    public long addCommunityTag(AddCommunityTagDTO addCommunityTagDTO) {
        if (addCommunityTagDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        if (addCommunityTagDTO.getTagcode() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签编码不能为空");
        }
        if (addCommunityTagDTO.getTagnamezh() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签名称不能为空");
        }
        QueryWrapper<CommunityTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tagcode", addCommunityTagDTO.getTagcode());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签编码已存在");
        }
        CommunityTag communityTag = new CommunityTag();
        BeanUtils.copyProperties(addCommunityTagDTO, communityTag);
        boolean result = this.save(communityTag);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "添加失败");
        }
        return communityTag.getId();
    }

    @Override
    public boolean deleteCommunityTag(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        QueryWrapper<CommunityTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        if (this.count(queryWrapper) <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签不存在");
        }
        return this.removeById(id);
    }

    @Override
    public boolean updateCommunityTag(UpdateCommunityTagDTO updateCommunityTagDTO) {
        if (updateCommunityTagDTO == null || updateCommunityTagDTO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        QueryWrapper<CommunityTag> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", updateCommunityTagDTO.getId());
        if (this.count(queryWrapper1) <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签不存在");
        }
        QueryWrapper<CommunityTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tagnamezh", updateCommunityTagDTO.getTagnamezh());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签名称已存在");
        }
        CommunityTag communityTag = new CommunityTag();
        BeanUtils.copyProperties(updateCommunityTagDTO, communityTag);
        return this.updateById(communityTag);
    }

    @Override
    public List<CommunityTag> listCommunityTags() {
        return this.list();
    }
}




